package com.ecarx.vhaltests.hy11.testcases.climate;

import android.car.VehicleAreaSeat;
import android.car.VehicleAreaType;
import android.car.hardware.CarPropertyValue;
import android.car.hardware.property.CarPropertyManager;
import android.util.Log;

import com.ecarx.vhaltests.IpcpClient;
import com.ecarx.vhaltests.VehicleConstants;
import com.ecarx.vhaltests.VhalUnitTestBase;
import com.ecarx.xui.adaptapi.car.IWrapper;
import com.ecarx.xui.adaptapi.car.hvac.IHvac;
import com.ecarx.xui.adaptapi.car.sensor.ISensor;
import com.ecarx.xui.adaptapi.car.sensor.ISensorEvent;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import androidx.test.platform.app.InstrumentationRegistry;


@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class HvacFuncAutoDehumidificationTest extends VhalUnitTestBase {
    private static final String TAG = "HvacFuncAutoDehumidificationTest";

    final static Object lock = new Object();
    static IpcpClient ipcpClient = new IpcpClient();
    private static IWrapper.IPropertyId dehumidificationPropertyId;
    static IWrapper wrapper;

    private static final int HVAC_FUNC_AREA = VehicleAreaSeat.SEAT_ROW_1_LEFT | VehicleAreaSeat.SEAT_ROW_1_RIGHT | VehicleAreaSeat.SEAT_ROW_2_LEFT
            | VehicleAreaSeat.SEAT_ROW_2_CENTER | VehicleAreaSeat.SEAT_ROW_2_RIGHT;

    @BeforeClass
    public static void testInit() {
        wrapper = com.ecarx.xui.adaptapi.car.Car.createWrapper(InstrumentationRegistry.getInstrumentation().getTargetContext());
        dehumidificationPropertyId = wrapper.getWrappedPropertyId(IWrapper.WrappedIdType.ID_TYPE_FUNCTION, IHvac.HVAC_FUNC_AUTO_DEHUMIDIFICATION);
        setConfig();
    }

    @Test
    public void test01_HvacFuncAutoDehumidificationAvail() {
        prepareHvacFuncAutoDehumidificationTestEnv(true);
        delay();
        testHvacFuncAutoDehumidificationAvail(true);

    }

    @Test
    public void test02_HvacFuncAutoDehumidificationNotAvail() {
        prepareHvacFuncAutoDehumidificationTestEnv(false);
        delay();
        testHvacFuncAutoDehumidificationAvail(false);
    }


    @Test
    public void test03_HvacFuncAutoDehumidificationOn() {
        prepareHvacFuncAutoDehumidificationTestEnv(true);
        delay();
        hvacFuncAutoDehumidificationSet(true);
    }

    @Test
    public void test04_HvacFuncAutoDehumidificationOff() {
        prepareHvacFuncAutoDehumidificationTestEnv(true);
        delay();
        hvacFuncAutoDehumidificationSet(false);
    }

    private void testHvacFuncAutoDehumidificationAvail(boolean except) {
        boolean isAvail = mCarPropertyMgr.isPropertyAvailable(dehumidificationPropertyId.getPropertyId(), HVAC_FUNC_AREA);
        Log.d(TAG, "testHvacFuncAutoDehumidificationAvail  isAvail：" + isAvail + " except： " + except);
        Assert.assertEquals(except, isAvail);
    }

    private void hvacFuncAutoDehumidificationSet(boolean value) {
        boolean bRet = false;
        Log.d(TAG, "HvacFuncAutoDehumidificationSet set value ：" + value);
        mCarPropertyMgr.setBooleanProperty(dehumidificationPropertyId.getPropertyId(), HVAC_FUNC_AREA, value);
        delay();
        boolean halValue = mCarPropertyMgr.getBooleanProperty(dehumidificationPropertyId.getPropertyId(), HVAC_FUNC_AREA);
        Assert.assertEquals(value, halValue);
    }

    private void prepareHvacFuncAutoDehumidificationTestEnv(boolean avail) {
        setCarConfig(2);
        if (avail) {
            setCEM(VehicleConstants.CAR_MODE_NOMAL, VehicleConstants.USG_MODE_DRIVING);
        } else {
            setCEM(VehicleConstants.CAR_MODE_NOMAL, VehicleConstants.USG_MODE_ABANDON);
        }
    }


    public void setCEM(int carMode, int usgMode) {
        int cemSvcId = 37;
        int cemOperId = 999;
        String[] cems = {"vehModMngtGlbSafe1CarModSts1", "vehModMngtGlbSafe1UsgModSts"};
        int[] cemValues = {carMode, usgMode};
        boolean cemResult = ipcpClient.setCEMSignal(cemSvcId, cemOperId, cems, cemValues);
        Assert.assertTrue(cemResult);
        delay();
    }

    public void setCarConfig(int carConfigValue) {
        int ccSvcId = 135;
        int ccOperId = 200;
        int[] configs = {353};
        int[] values = {carConfigValue};
        boolean ccResult = ipcpClient.setCarconfigSignal(ccSvcId, ccOperId, configs, values);
        Assert.assertTrue(ccResult);
        delay();
    }
    private static void setConfig() {
        // HY11 car mode & usage mode 信号对应 ECU 为 CEM
        int cemSvcId = 37;
        int[] cemOperIds = {999};
        // carconfig 对应 Service ID & Operation ID
        int ccSvcId = 135;
        int[] ccOperIds = {200};

        boolean retCem = ipcpClient.setConfig(cemSvcId, cemOperIds);
        Assert.assertTrue(retCem);

        boolean retCc = ipcpClient.setConfig(ccSvcId, ccOperIds);
        Assert.assertTrue(retCc);

    }


}
